package com.unittec.zk.provider.work.controller;

import com.alibaba.fastjson.JSON;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.provider.work.service.CarApplicationService;
import com.unittec.zk.sdk.log.enums.LogOperateEnum;
import com.unittec.zk.sdk.log.enums.LogStatusEnum;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.enums.BizCodeEnum;
import com.unittec.zk.sdk.work.request.Req202004;
import com.unittec.zk.sdk.work.request.Req202005;
import com.unittec.zk.sdk.work.request.Req202006;
import com.unittec.zk.sdk.work.request.Req202007;
import com.unittec.zk.sdk.work.response.Res202000;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "用车申请管理")
public class CarApplicationController {
    @Resource
    private Log log;
    
    @Resource
    private CarApplicationService service;
    
    //用车申请-增/改
    @RequestMapping(value = "/202004", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用车申请-增/改", notes = "用车申请-增/改")
    public Result<Res202000> ApplicationSaveOrUpdate(@RequestBody GatewayRequest<Req202004> req) {
        log.debug(req, "ApplicationSaveOrUpdate {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = service.ApplicationSaveOrUpdate(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202004", JSON.toJSONString(req), "用车申请-增/改", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "ApplicationSaveOrUpdate {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202004", JSON.toJSONString(req), "用车申请-增/改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202004", JSON.toJSONString(req), "用车申请-增/改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //人员选择
    @RequestMapping(value = "/202005", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "人员选择", notes = "人员选择")
    public Result<Res202000> selectUser(@RequestBody GatewayRequest<Req202005> req) {
        log.debug(req, "selectUser {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = service.selectUser(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202005", JSON.toJSONString(req), "人员选择", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "selectUser {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202005", JSON.toJSONString(req), "人员选择", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202005", JSON.toJSONString(req), "人员选择", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //用车申请-分页
    @RequestMapping(value = "/202006", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用车申请-分页", notes = "用车申请-分页")
    public Result<Res202000> getApplicationPage(@RequestBody GatewayRequest<Req202006> req) {
        log.debug(req, "getApplicationPage {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = service.getApplicationPage(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202006", JSON.toJSONString(req), "用车申请-分页", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getApplicationPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202006", JSON.toJSONString(req), "用车申请-分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202006", JSON.toJSONString(req), "用车申请-分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //用车申请-逻辑删除
    @RequestMapping(value = "/202007", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用车申请-逻辑删除", notes = "用车申请-逻辑删除")
    public Result<Res202000> delApplicationByIds(@RequestBody GatewayRequest<Req202007> req) {
        log.debug(req, "delApplicationByIds {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = service.delApplicationByIds(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202007", JSON.toJSONString(req), "用车申请-逻辑删除", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "delApplicationByIds {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202007", JSON.toJSONString(req), "用车申请-逻辑删除", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202007", JSON.toJSONString(req), "用车申请-逻辑删除", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
}
